//-----------------------------------------------------------------------
// <copyright file="CatalogDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class CatalogDAOBase
    {
        #region Common methods
        public virtual Catalog CreateCatalogFromReader(IDataReader reader)
        {
            Catalog item = new Catalog();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CatalogId"))) item.CatalogId = (long)reader["CatalogId"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatalogName"))) item.CatalogName = (string)reader["CatalogName"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatalogLink"))) item.CatalogLink = (string)reader["CatalogLink"];
                if (!reader.IsDBNull(reader.GetOrdinal("Target"))) item.Target = (string)reader["Target"];
                if (!reader.IsDBNull(reader.GetOrdinal("Sort"))) item.Sort = (long)reader["Sort"];
                if (!reader.IsDBNull(reader.GetOrdinal("Activate"))) item.Activate = (bool)reader["Activate"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("LastActivityDate"))) item.LastActivityDate = (DateTime)reader["LastActivityDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("Picture_S"))) item.Picture_S = (string)reader["Picture_S"];
                if (!reader.IsDBNull(reader.GetOrdinal("Picture_L"))) item.Picture_L = (string)reader["Picture_L"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Content"))) item.Content = (string)reader["Content"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("High_Level_Catalog"))) item.High_Level_Catalog = (long)reader["High_Level_Catalog"];
                if (!reader.IsDBNull(reader.GetOrdinal("Level"))) item.Level = (long)reader["Level"];
                if (!reader.IsDBNull(reader.GetOrdinal("Catalog_Setup_Id"))) item.Catalog_Setup_Id = (long)reader["Catalog_Setup_Id"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatalogName_eg"))) item.CatalogName_eg = (string)reader["CatalogName_eg"];
                if (!reader.IsDBNull(reader.GetOrdinal("Picture_S_eg"))) item.Picture_S_eg = (string)reader["Picture_S_eg"];
                if (!reader.IsDBNull(reader.GetOrdinal("Picture_L_eg"))) item.Picture_L_eg = (string)reader["Picture_L_eg"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title_eg"))) item.Title_eg = (string)reader["Title_eg"];
                if (!reader.IsDBNull(reader.GetOrdinal("Content_eg"))) item.Content_eg = (string)reader["Content_eg"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description_eg"))) item.Description_eg = (string)reader["Description_eg"];
                if (!reader.IsDBNull(reader.GetOrdinal("HighLight"))) item.HighLight = (bool)reader["HighLight"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCatalogFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCatalog methods
            
        public virtual void CreateCatalog(Catalog catalog)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCatalogsInsert");
                
                database.AddInParameter(dbCommand, "@CatalogName", DbType.String, catalog.CatalogName);
                database.AddInParameter(dbCommand, "@CatalogLink", DbType.String, catalog.CatalogLink);
                database.AddInParameter(dbCommand, "@Target", DbType.String, catalog.Target);
                database.AddInParameter(dbCommand, "@Sort", DbType.Int64, catalog.Sort);
                database.AddInParameter(dbCommand, "@Activate", DbType.Boolean, catalog.Activate);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, catalog.Enable);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, catalog.CreatedDate);
                database.AddInParameter(dbCommand, "@LastActivityDate", DbType.DateTime, catalog.LastActivityDate);
                database.AddInParameter(dbCommand, "@Picture_S", DbType.String, catalog.Picture_S);
                database.AddInParameter(dbCommand, "@Picture_L", DbType.String, catalog.Picture_L);
                database.AddInParameter(dbCommand, "@Title", DbType.String, catalog.Title);
                database.AddInParameter(dbCommand, "@Content", DbType.String, catalog.Content);
                database.AddInParameter(dbCommand, "@Description", DbType.String, catalog.Description);
                database.AddInParameter(dbCommand, "@High_Level_Catalog", DbType.Int64, catalog.High_Level_Catalog);
                database.AddInParameter(dbCommand, "@Level", DbType.Int64, catalog.Level);
                database.AddInParameter(dbCommand, "@Catalog_Setup_Id", DbType.Int64, catalog.Catalog_Setup_Id);
                database.AddInParameter(dbCommand, "@CatalogName_eg", DbType.String, catalog.CatalogName_eg);
                database.AddInParameter(dbCommand, "@Picture_S_eg", DbType.String, catalog.Picture_S_eg);
                database.AddInParameter(dbCommand, "@Picture_L_eg", DbType.String, catalog.Picture_L_eg);
                database.AddInParameter(dbCommand, "@Title_eg", DbType.String, catalog.Title_eg);
                database.AddInParameter(dbCommand, "@Content_eg", DbType.String, catalog.Content_eg);
                database.AddInParameter(dbCommand, "@Description_eg", DbType.String, catalog.Description_eg);
                database.AddInParameter(dbCommand, "@HighLight", DbType.Boolean, catalog.HighLight);
                database.AddOutParameter(dbCommand, "@CatalogId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                catalog.CatalogId = (long)database.GetParameterValue(dbCommand, "@CatalogId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCatalogException, ex);
            }
        }

        #endregion

        #region UpdateCatalog methods
        
        public virtual void UpdateCatalog(Catalog catalog)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCatalogsUpdate");            
                
                database.AddInParameter(dbCommand, "@CatalogId", DbType.Int64, catalog.CatalogId);
                database.AddInParameter(dbCommand, "@CatalogName", DbType.String, catalog.CatalogName);
                database.AddInParameter(dbCommand, "@CatalogLink", DbType.String, catalog.CatalogLink);
                database.AddInParameter(dbCommand, "@Target", DbType.String, catalog.Target);
                database.AddInParameter(dbCommand, "@Sort", DbType.Int64, catalog.Sort);
                database.AddInParameter(dbCommand, "@Activate", DbType.Boolean, catalog.Activate);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, catalog.Enable);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, catalog.CreatedDate);
                database.AddInParameter(dbCommand, "@LastActivityDate", DbType.DateTime, catalog.LastActivityDate);
                database.AddInParameter(dbCommand, "@Picture_S", DbType.String, catalog.Picture_S);
                database.AddInParameter(dbCommand, "@Picture_L", DbType.String, catalog.Picture_L);
                database.AddInParameter(dbCommand, "@Title", DbType.String, catalog.Title);
                database.AddInParameter(dbCommand, "@Content", DbType.String, catalog.Content);
                database.AddInParameter(dbCommand, "@Description", DbType.String, catalog.Description);
                database.AddInParameter(dbCommand, "@High_Level_Catalog", DbType.Int64, catalog.High_Level_Catalog);
                database.AddInParameter(dbCommand, "@Level", DbType.Int64, catalog.Level);
                database.AddInParameter(dbCommand, "@Catalog_Setup_Id", DbType.Int64, catalog.Catalog_Setup_Id);
                database.AddInParameter(dbCommand, "@CatalogName_eg", DbType.String, catalog.CatalogName_eg);
                database.AddInParameter(dbCommand, "@Picture_S_eg", DbType.String, catalog.Picture_S_eg);
                database.AddInParameter(dbCommand, "@Picture_L_eg", DbType.String, catalog.Picture_L_eg);
                database.AddInParameter(dbCommand, "@Title_eg", DbType.String, catalog.Title_eg);
                database.AddInParameter(dbCommand, "@Content_eg", DbType.String, catalog.Content_eg);
                database.AddInParameter(dbCommand, "@Description_eg", DbType.String, catalog.Description_eg);
                database.AddInParameter(dbCommand, "@HighLight", DbType.Boolean, catalog.HighLight);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCatalogException, ex);
            }
        }
        
        #endregion

        #region DeleteCatalog methods
        public virtual void DeleteCatalog(long catalogId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCatalogsDelete");
                
                database.AddInParameter(dbCommand, "@CatalogId", DbType.Int64, catalogId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCatalogException, ex);
            }
        }

        #endregion

        #region GetCatalog methods
        
        public virtual Catalog GetCatalog(long catalogId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCatalogsGet");
                
                database.AddInParameter(dbCommand, "@CatalogId", DbType.Int64, catalogId);
                
                Catalog catalog =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        catalog = CreateCatalogFromReader(reader);
                        reader.Close();
                    }
                }
                return catalog;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCatalogException, ex);
            }
        }
        
        #endregion

        #region GetCatalogList methods
        public virtual CatalogCollection GetCatalogList(CatalogColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCatalogsGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CatalogCollection catalogCollection = new CatalogCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Catalog catalog = CreateCatalogFromReader(reader);
                        catalogCollection.Add(catalog);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return catalogCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCatalogListException, ex);
            }
        }
        
        public virtual CatalogCollection GetCatalogList(CatalogColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCatalogList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetCatalogList methods
        public virtual CatalogCollection GetCatalogListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCatalogsGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CatalogCollection catalogCollection = new CatalogCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Catalog catalog = CreateCatalogFromReader(reader);
                        catalogCollection.Add(catalog);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return catalogCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCatalogListException, ex);
            }
        }
        
        public virtual CatalogCollection GetCatalogListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetCatalogListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}